﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MvcMall.AdminWeb.DtoModels;
using MvcMall.Core.Contacts.Tasks;
using MvcMall.Framework.Extensions;
using MvcMall.Framework.MvcPager;

namespace MvcMall.AdminWeb.Manage.Articles
{
    public partial class Index : ManageBasePage
    {
        private IArticleCategoryTask _articleCatTask;
        private IArticleTask _articleTask;

        protected void Page_Load(object sender, EventArgs e)
        {
            ValidatePagePower("ArticleManage");

            _articleCatTask = GetInstance<IArticleCategoryTask>();
            _articleTask = GetInstance<IArticleTask>();

            if (!IsPostBack)
            {
                ddlCats.DataSource = _articleCatTask.GetAll().ToSelectList(m => m.Id.ToString(), m => m.CategoryName);
                ddlCats.DataBind();
                ddlCats.Items.Insert(0, new ListItem("全部分类", "-1"));

                BindRpt();
            }
        }

        private void BindRpt()
        {
            ANPager.Attributes["ArticleCatId"] = ddlCats.SelectedValue;
            ANPager.Attributes["Title"] = txtSearchTitle.Text.Trim();

            var searchCatId = Convert.ToInt32(ddlCats.SelectedValue);
            var searchTitle = txtSearchTitle.Text.Trim();

            var list = _articleTask.GetPagedList(ANPager.CurrentPageIndex, ANPager.PageSize, searchCatId, searchTitle);
            var dtoList = list.Select(c => new ArticleDto
                                               {
                                                   CategoryName = c.ArticleCategory.CategoryName,
                                                   Content = c.Content,
                                                   CreateTime = c.CreateTime,
                                                   Id = c.Id,
                                                   IsShow = c.IsShow,
                                                   Title = c.Title
                                               })
                .ToList();
            rptCats.DataSource = dtoList;
            rptCats.DataBind();
            ANPager.RecordCount = list.TotalItemCount;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindRpt();
        }

        protected void ANPager_PageChanged(object sender, EventArgs e)
        {
            BindRpt();
        }


    }
}